package com.bluezone.dec.system.controller;

import com.bluezone.dec.common.JsonResult;
import com.bluezone.dec.common.PageEntity;
import com.bluezone.dec.common.SystemConstant;
import com.bluezone.dec.system.entity.EntRectRecord;
import com.bluezone.dec.system.service.EntRectRecordService;
import com.bluezone.dec.utils.UUIDUtils;
import com.google.common.base.Strings;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author HHL
 * @description 专项整治记录
 * @date 2018/10/12 - 10:13
 */
@Api(tags = "专项行动数据录入/专项整治记录")
@RestController
@RequestMapping("/dec/record")
public class SpecialRectificationRecordController {

    private static Logger log = LoggerFactory.getLogger(SpecialRectificationRecordController.class);

    @Autowired
    private EntRectRecordService entRectRecordService;

    @ApiOperation("根据传递的参数，分页查询专项整治记录数据")
    @GetMapping("/records-page")
    public JsonResult getRecordListByPage(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = SystemConstant.PAGE_SIZE) int pageSize,
            @RequestParam(value = "entId", required = false) String entCode,
            @RequestParam(value = "planId", required = false) String planId,
            @RequestParam(value = "rectLevel", required = false) String rectLevel,
            @RequestParam(value = "status", required = false) String status) {

        Map<String, Object> params = new HashMap<>();
        PageEntity<EntRectRecord> recordListPage = null;

        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        params.put("entCode", entCode);
        params.put("planId", planId);
        params.put("rectLevel", rectLevel);
        params.put("status", status);

        if (params.size() == 0) {
            return new JsonResult().paramsError();
        }

        try {
            recordListPage = entRectRecordService.findByParams(params);
        } catch (Exception e) {
            log.warn("数据库里记录表没有数据，无法进行分页哟", e);
        }

        return new JsonResult().ok(recordListPage);
    }

    @ApiOperation("根据企业专项整治记录id删除企业专项整治记录")
    @PostMapping("/delete")
    public JsonResult deleteRecordById(@RequestBody List<String> ids) {
        int i = entRectRecordService.deleteRecordByRecordId(ids);
        if (i == 0) {
            return new JsonResult().error();
        }
        return new JsonResult().ok();
    }

    @ApiOperation("根据记录id更新企业专项整治记录")
    @PostMapping("/update")
    public JsonResult updateRecordById(@RequestBody EntRectRecord entRectRecord) {

        if (Strings.isNullOrEmpty(entRectRecord.getId())) {
            return new JsonResult().error();
        }
        int i = entRectRecordService.update(entRectRecord);

        if (i > 0) {
            return new JsonResult().ok();
        }

        return new JsonResult().error();
    }

    @ApiOperation("查询企业专项整治记录表里存在的所有企业")
    @GetMapping("/ents-from-record")
    public JsonResult findAllEnterprise() {

        List<EntRectRecord> enterpriseList = entRectRecordService.findAllEnterprise();

        if (StringUtils.isEmpty(enterpriseList)) {
            return new JsonResult().none();
        }

        return new JsonResult().ok(enterpriseList);
    }

    @ApiOperation("添加企业专项整治记录")
    @PostMapping("/add")
    public JsonResult addEntRectRecord(@RequestBody EntRectRecord entRectRecord) {

        if (entRectRecord != null) {

            entRectRecord.setId(UUIDUtils.randomUUID());
            entRectRecord.setUpdateTime(new Date());

            try {
                entRectRecordService.save(entRectRecord);
            } catch (Exception e) {
                log.warn("企业专项整治记录添加失败", e);
            }
        }
        return new JsonResult().ok();
    }
}
